package com.fh.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Qiu,Junda 
 * @date 2019年4月9日
 * @description
 */
public class StringUtils {

	private StringUtils() {
        throw new AssertionError();
    }

    /**
     * 
     */
    public static boolean isEmpty(String str) {
        return (str == null || str.length() == 0||str.trim().equals(""));
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean compareStr(String str1,String str2){
        if(str1==null){
            return str2==null?true:false;
        }else{
            return str1.equals(str2);
        }

    }

    public static boolean comareInteger(Integer int1,Integer int2){
        if(int1==null){
            return int2==null?true:false;
        }else{
            return int2==null?false:(int1.intValue()==int2.intValue());
        }
    }

    public static String toUpperCase(String str){
        return str==null?str:str.toUpperCase();
    }

    public static String toLowerCase(String str){
        return str==null?str:str.toLowerCase();
    }

    /**
     * @description 首字母大写
     */
    public static String upperFirst(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char c = str.charAt(0);
        return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str : new StringBuilder(str.length())
                .append(Character.toUpperCase(c)).append(str.substring(1)).toString();
    }
    /**
     * @description 首字母小写
     */
    public static String lowerFirst(String str) {
        if (isEmpty(str)) {
            return str;
        }
        char c = str.charAt(0);
        return (!Character.isLetter(c) || Character.isLowerCase(c)) ? str : new StringBuilder(str.length())
                .append(Character.toLowerCase(c)).append(str.substring(1)).toString();
    }

    public static void main(String[] args){
        System.out.println(lowerFirst("XXSdfasdfXsdfsdf_sdf"));
    }

    /**
     * encoded in utf-8
     */
    public static String utf8Encode(String str) {
        if (!isEmpty(str) && str.getBytes().length != str.length()) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
            }
        }
        return str;
    }

    /**
     * encoded in utf-8, if exception, return defultReturn
     */
    public static String utf8Encode(String str, String defultReturn) {
        if (!isEmpty(str) && str.getBytes().length != str.length()) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                return defultReturn;
            }
        }
        return str;
    }

    /**
     * get innerHtml from href
     */
    public static String getHrefInnerHtml(String href) {
        if (isEmpty(href)) {
            return "";
        }

        String hrefReg = ".*<[\\s]*a[\\s]*.*>(.+?)<[\\s]*/a[\\s]*>.*";
        Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);
        Matcher hrefMatcher = hrefPattern.matcher(href);
        if (hrefMatcher.matches()) {
            return hrefMatcher.group(1);
        }
        return href;
    }

    /**
     * 
     */
    public static String htmlEscapeCharsToString(String source) {
        return StringUtils.isEmpty(source) ? source : source.replaceAll("&lt;", "<").replaceAll("&gt;", ">")
                .replaceAll("&amp;", "&").replaceAll("&quot;", "\"");
    }

    /**
     * @description 全角转换半角
     */
    public static String fullWidthToHalfWidth(String s) {
        if (isEmpty(s)) {
            return s;
        }
        char[] source = s.toCharArray();
        for (int i = 0; i < source.length; i++) {
            if (source[i] == 12288) {
                source[i] = ' ';
                // } else if (source[i] == 12290) {
                // source[i] = '.';
            } else if (source[i] >= 65281 && source[i] <= 65374) {
                source[i] = (char)(source[i] - 65248);
            } else {
                source[i] = source[i];
            }
        }
        return new String(source);
    }

    /**
     * @description 半角转换全角
     */
    public static String halfWidthToFullWidth(String s) {
        if (isEmpty(s)) {
            return s;
        }
        char[] source = s.toCharArray();
        for (int i = 0; i < source.length; i++) {
            if (source[i] == ' ') {
                source[i] = (char)12288;
                // } else if (source[i] == '.') {
                // source[i] = (char)12290;
            } else if (source[i] >= 33 && source[i] <= 126) {
                source[i] = (char)(source[i] + 65248);
            } else {
                source[i] = source[i];
            }
        }
        return new String(source);
    }

    /**
     *
     * <前台传过来的多参数  格式化>
     *     str--前台传过来的多参数
     *     flag=true  格式化后加上引号;  flag=false  格式化后不加引号
     */
    public static String convertString(String str,boolean flag) {
        //去掉 ' 和  ,  然后判断数组
        if(str==null||"".equals(str)){
            return str;
        }
        String[] list = str.replaceAll("'","" ).split(",");
        String returnStr = "";
        for(int i=0;i<list.length;i++){
            String strType="";
            if(flag){
                strType="'"+list[i]+"'";
            }else{
                strType=list[i];
            }
            //如果不是最后一位  拼接的时候前几个 加逗号分隔
            if(i<list.length-1){
                returnStr += strType+",";
            }else{
                returnStr += strType;
            }
        }
        return returnStr;
    }
    
}
